<?php
class product_model extends jqxGrid_CI_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    function get($ID){
        $where=array('ProductID'=>$ID);
        $query=$this->db->get_where("dtb_product", $where); 
        return $query->result();
    }
    function getByAlias($Alias){
        $where=array('Alias'=>$Alias,"Delete"=>null);
        $query=$this->db
                ->where($where)
                ->where("(`Status`='true' or `Status`='Public')")
                ->get("dtb_product"); 
        return $query->result_array();
    }
    function gets($category="tat-ca",$begin=0,$num=68)
    {
        $re=  isset($_SESSION["DRegion"])?$_SESSION["DRegion"]:"";
        $category= mysql_escape_string($category);
        
        $query = $this->db->query("
            SELECT `dtb_product`.*
            FROM `dtb_product` 
                ".($category!="tat-ca"?"
                    INNER JOIN dtb_category 
                    ON (`dtb_product`.Categorys like concat('%--',dtb_category.`id`,'--%') 
                        AND dtb_category.`delete` is null)":"")."
            WHERE `dtb_product`.`Delete` is null
                ".($category!="tat-ca"?"AND dtb_category.alias='$category'":"")."
                AND NOW() BETWEEN dtb_product.StartDate AND dtb_product.EndDate
                AND (`dtb_product`.`Status`='Public' or `dtb_product`.`Status`='true')
                AND `dtb_product`.`Delete` is null
                ".($re!=""?"AND `dtb_product`.`Regions` like '%--$re--%'":"")."
            ORDER BY `dtb_product`.`StartDate` DESC
            LIMIT $begin,$num
        ");
        //$query=$this->db->get();
        //echo $this->db->last_query();
        return $query->result_array();
    }
    function counts($category="tat-ca")
    {
        
        $re=  isset($_SESSION["DRegion"])?$_SESSION["DRegion"]:"";
        $category= mysql_escape_string($category);
        
        $query = $this->db->query("
            SELECT count(*) as 'numrow'
            FROM `dtb_product` 
                ".($category!="tat-ca"?"
                    INNER JOIN `dtb_category`
                    ON (`dtb_product`.`Categorys` like concat('%--',`dtb_category`.`id`,'--%') 
                        AND `dtb_category`.`delete` is null)":"")."
            WHERE `dtb_product`.`Delete` is null 
                ".($category!="tat-ca"?"AND `dtb_category`.`alias`='$category'":"")."
                AND NOW() BETWEEN `dtb_product`.`StartDate` AND `dtb_product`.`EndDate`
                AND (`dtb_product`.`Status`='Public' or `dtb_product`.`Status`='true')
                ".($re!=""?"AND `dtb_product`.`Regions` like '%--$re--%'":"")."
            
        ");
        $n = $query->result();
        //echo $this->db->last_query();
        return $n;
    }
    function getByID($ID){
        $where=array('ProductID'=>$ID,"Delete"=>null);
        $query=$this->db
                ->where($where)
                ->where("(`Status`='true' or `Status`='Public')")
                ->get("dtb_product"); 
        return $query->result_array();
    }
    function insert($params){
        $this->db->set('Insert', 'NOW()', FALSE);
        $this->db->insert("dtb_product", $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function delete($ID){
        $this->db->set('Delete', 'NOW()', FALSE);
        $where=array('ProductID'=>$ID);
        $this->db->where($where);
        $this->db->update("dtb_product"); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function retore($ID){
        $this->db->set('Delete', 'NULL', FALSE);
        $where=array('ProductID'=>$ID);
        $this->db->where($where);
        $this->db->update("dtb_product"); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function update($ID,$params){
        $this->db->set('Update', 'NOW()', FALSE);
        $this->db->where('ProductID', $ID);
        $this->db->update("dtb_product", $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function updateSold($ID,$sold){
        $this->db->set('Update', 'NOW()', FALSE);
        $this->db->set('Sold', "`Sold`+$sold", FALSE);
        $this->db->where('ProductID', $ID);
        $this->db->update("dtb_product"); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function getProductToken($q)
    {
        $where=array(
            'Delete'=>NULL
            //,"Status"=>"true"
            );
        $query=$this->db
                ->select("`ProductID`,`ProductID` as 'id',`ProductName`")
                ->where($where)
                ->like("ProductName","$q")
                ->get("dtb_product",15); 
        return $query->result();
    }
    function getProductInList($list)
    {
        $where=array(
            'Delete'=>NULL
            //,"Status"=>"true"
            );
        $query=$this->db
                ->where($where)
                ->where_in("ProductID",$list)
                ->get("dtb_product",15); 
        return $query->result();
    }
    function binding(){
        $strQuery="
            SELECT SQL_CALC_FOUND_ROWS dtb_product.*,dtb_product.ProductID as ID
            FROM `dtb_product`
            ";
        $strWhere="
            WHERE  true
            ";
        if($_SESSION["ADP"][CCTRL]["display"]==0){
            $strWhere.=" AND `dtb_product`.`Delete` IS NULL";
        }elseif($_SESSION["ADP"][CCTRL]["display"]==-1){
            $strWhere.=" AND `dtb_product`.`Delete` IS NOT NULL";
        }
        $strOrderBy="Order By dtb_product.`Insert` DESC ";
        $strGroupBy="";
        $config=array(
            "strQuery"=>$strQuery,
            "strWhere"=>$strWhere,
            "strOrderBy"=>$strOrderBy,
            "strGroupBy"=>$strGroupBy,
            "usingLimit"=>true,
            "fields"=>array(
                "ID"=>"`ProductID`"
            ),
            "datefields"=>array("Insert","Update","Delete","StartDate","EndDate")
        );
        $this->init($config);
        return $this->jqxBinding();
    }
}
?>
